package jmu.edu.yjc.service.answer;

import org.neo4j.driver.Driver;
import org.neo4j.driver.Record;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author yjc
 * @date 2023/3/12 15:06
 */
@Component
public class AnswerParser {
    @Autowired
    private Driver driver;
    /***
     * @description: 返回处理完之后应答给用户的语句
     * @param: sqls
     * @return: List<String>
     * @author yjc
     * @date: 2023/3/12 17:11
     */
    public List<String> finalAnswer(Map<String,List<String>> sqls){
        List<String> final_answer = new ArrayList<>();
        try (Session session = driver.session()) {
            for (Map.Entry<String, List<String>> entry : sqls.entrySet()) {
                String questionType = entry.getKey();
                List<String> sql = entry.getValue();
                List<Result> result = new ArrayList<>();
                for (String s : sql) {
                    result.add(session.run(s));
                }
                String answerTemplate = parserAnswerTemplate(questionType, result);
                if(StringUtils.hasText(answerTemplate)) {
                    final_answer.add(answerTemplate);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return final_answer;
    }
    /***
     * @description: 根据问题类型讲查出的数据处理成对应模板
     * @param: questionType
results
     * @return: String
     * @author yjc
     * @date: 2023/3/12 17:12
     */
    private String parserAnswerTemplate(String questionType, List<Result> results){
        String finalTemplate = "";
        List<String> template = new ArrayList<>();
        if(results.size()<=0){
            return "";
        }
        if(!results.get(0).hasNext()){
            return "";
        }
        for (Result result : results) {
            List<Record> records = result.list();
            if(questionType.equals("disease_symptom")){
                String subject = records.get(0).get("m.name").asString();
                String parseStr = parserNRecord(records);
                if(StringUtils.hasText(parseStr)){
                    template.add(subject+"的症状包括：" + parseStr + "等");
                }
            }else if(questionType.equals("symptom_disease")){
                String subject = records.get(0).get("n.name").asString();
                String parseStr = parserMRecord(records);
                if(StringUtils.hasText(parseStr)){
                    template.add("症状"+subject+"可能感染的疾病有："+parseStr+"等等");
                }
            }else if(questionType.equals("disease_cause")){
                String subject = records.get(0).get("m.name").asString();
                String parseStr = records.get(0).get("m.cause").asString();
                if(StringUtils.hasText(parseStr)){
                    template.add(subject+"可能的成因：" + parseStr);
                }
            }else if(questionType.equals("disease_prevent")){
                String subject = records.get(0).get("m.name").asString();
                String parseStr = records.get(0).get("m.prevent").asString();
                if(StringUtils.hasText(parseStr)){
                    template.add(subject+"可以这么预防：" + parseStr);
                }
            }else if(questionType.equals("disease_lasttime")){
                String subject = records.get(0).get("m.name").asString();
                String parseStr = records.get(0).get("m.cureLasttime").asString();
                if(StringUtils.hasText(parseStr)){
                    template.add(subject+"的治疗时间最少为：" + parseStr);
                }
            }else if(questionType.equals("disease_cureway")){
                String subject = records.get(0).get("m.name").asString();
                String parseStr = records.get(0).get("m.cureWay").asString();
                if(StringUtils.hasText(parseStr)){
                    template.add(subject+"可以尝试如下治疗：" + parseStr);
                }
            }else if(questionType.equals("disease_cureprob")){
                String subject = records.get(0).get("m.name").asString();
                String parseStr = records.get(0).get("m.curedProb").asString();
                if(StringUtils.hasText(parseStr)){
                    template.add(subject+"治愈的概率为（仅供参考）：" + parseStr);
                }
            }else if(questionType.equals("disease_easyget")){
                String subject = records.get(0).get("m.name").asString();
                String parseStr = records.get(0).get("m.easyGet").asString();
                if(StringUtils.hasText(parseStr)){
                    template.add(subject+"的易感人群包括：" + parseStr);
                }
            }else if(questionType.equals("disease_desc")){
                String subject = records.get(0).get("m.name").asString();
                String parseStr = records.get(0).get("m.desc").asString();
                if(StringUtils.hasText(parseStr)){
                    template.add(subject+"：" + parseStr);
                }
            }else if(questionType.equals("disease_acompany")){
                String subject = records.get(0).get("m.name").asString();
                String parseStr = parserNRecord(records);
                if(StringUtils.hasText(parseStr)){
                    template.add(subject+"的并发症包括：" + parseStr);
                }
            }else if(questionType.equals("disease_not_food")){
                String subject = records.get(0).get("m.name").asString();
                String parseStr = parserNRecord(records);
                if(StringUtils.hasText(parseStr)){
                    template.add(subject+"忌食的食物包括有：" + parseStr);
                }
            }else if(questionType.equals("disease_do_food")){
                String subject = records.get(0).get("m.name").asString();
                String relation = records.get(0).get("r.name").asString();
                if(relation.equals("宜吃")){
                    String parseStr = parserNRecord(records);
                    if(StringUtils.hasText(parseStr)){
                        template.add(subject+"宜吃的食物包括有：" + parseStr);
                    }
                }else {
                    String parseStr = parserNRecord(records);
                    if(StringUtils.hasText(parseStr)){
                        template.add(subject+"推荐吃的食物包括有：" + parseStr);
                    }
                }
            }else if(questionType.equals("food_do_disease")){
                String subject = records.get(0).get("n.name").asString();
                String parseStr = parserMRecord(records);
                if(StringUtils.hasText(parseStr)){
                    template.add("患有"+parseStr+"可以多吃吃" + subject);
                }
            }else if(questionType.equals("food_not_disease")){
                String subject = records.get(0).get("n.name").asString();
                String parseStr = parserMRecord(records);
                if(StringUtils.hasText(parseStr)){
                    template.add("患有" + parseStr + "最好不要吃" + subject);
                }
            }else if(questionType.equals("disease_drug")){
                String subject = records.get(0).get("m.name").asString();
                String relation = records.get(0).get("r.name").asString();
                if(relation.equals("常规用药")){
                    String parseStr = parserNRecord(records);
                    if(StringUtils.hasText(parseStr)){
                        template.add(subject+"通常的使用的药品包括：" + parseStr);
                    }
                }else {
                    String parseStr = parserNRecord(records);
                    if(StringUtils.hasText(parseStr)){
                        template.add(subject+"推荐使用的药品包括：" + parseStr);
                    }
                }
            }else if(questionType.equals("drug_disease")){
                String subject = records.get(0).get("n.name").asString();
                String parseStr = parserMRecord(records);
                if(StringUtils.hasText(parseStr)){
                    template.add(subject+"主治的疾病有：" + parseStr);
                }
            }else if(questionType.equals("disease_check")){
                String subject = records.get(0).get("m.name").asString();
                String parseStr = parserMRecord(records);
                if(StringUtils.hasText(parseStr)){
                    template.add(subject+"通常可以通过以下方式检查出来：" + parseStr);
                }
            }else if(questionType.equals("check_disease")){
                String subject = records.get(0).get("n.name").asString();
                String parseStr = parserMRecord(records);
                if(StringUtils.hasText(parseStr)){
                    template.add("通常可以通过"+subject+"检查出来的疾病包括："+parseStr);
                }
            }
        }
        finalTemplate = String.join(";",template);
        return finalTemplate;
    }
    private String parserNRecord(List<Record> records){
        String returnStr = "";
        List<String> names = new ArrayList<>();
        for (Record record : records) {
            names.add(record.get("n.name").asString());
        }
        returnStr = String.join(",",names);
        return returnStr;
    }
    private String parserMRecord(List<Record> records){
        String returnStr = "";
        List<String> names = new ArrayList<>();
        for (Record record : records) {
            names.add(record.get("m.name").asString());
        }
        returnStr = String.join(",",names);
        return returnStr;
    }
}
